//
//  JCollectionHeadView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/24.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JCollectionHeadView.h"
#import "JButton.h"

#define miaoshaHeight 160

@interface  JCollectionHeadView()
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *scrBgView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, assign) CGFloat minY;

@property (nonatomic, assign) BOOL isFirst;

@end

@implementation JCollectionHeadView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        _isFirst = NO;
        [self setUpView];
        
    }
    return  self;
}

- (void)setUpView
{
    _killImgArr = [NSMutableArray array];
    _bannerArr = [NSArray array];
    _kindNameArr = [NSArray array];
    // 轮播图
    [self setUpRunLoopImg];
    
    [self loadAdviceArr];
    
    // 种类
    [self loadAdviceKindArr];
//    // 种类按钮
//    [self setUpKindButton];
    
    [self setUpTitleView];
    
    [self loadKillView];
}

- (void)setUpTitleView
{
    UIView *linebgView = [[UIView alloc]init];
    linebgView.left = 0;
    linebgView.top = self.height - 40 - 20;
    linebgView.size = CGSizeMake(kWindowW, 60);
    [self addSubview:linebgView];
    _lineView = linebgView;

    
    UIView *lineView = [[UIView alloc]init];
    lineView.left = 0;
    lineView.top = 0;
    lineView.size = CGSizeMake(kWindowW, 15);
    lineView.backgroundColor = kRGBColor(238, 238, 238);
    [linebgView addSubview:lineView];
    
    UIView *titleView = [[UIView alloc]init];
    titleView.left = 0;
    titleView.top = 20;
    titleView.size = CGSizeMake(kWindowW, 40);
    titleView.backgroundColor = [UIColor whiteColor];
    [linebgView addSubview:titleView];
    
    YYLabel *titleLabel = [YYLabel new];
    titleLabel.centerX = titleView.centerX;
    titleLabel.top = 25;
    titleLabel.size = CGSizeMake(100, 30);
    titleLabel.text = @"热销产品";
    [titleLabel sizeToFit];
    titleLabel.top = (titleView.height - titleLabel.height)/2.0 +5;
    titleLabel.centerX = titleView.centerX;
    titleLabel.size = CGSizeMake(titleLabel.width, titleLabel.height);
    [titleView addSubview:titleLabel];
    
    CALayer *imgLayer = [CALayer layer];
    imgLayer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"likely"].CGImage);
    imgLayer.right = titleLabel.left - 30;
    imgLayer.top = titleView.height - 25;
    imgLayer.size = CGSizeMake(20, 20);
    [titleView.layer addSublayer:imgLayer];
}

// 轮播图
- (void)setUpRunLoopImg
{
    NSArray *images = @[
                        [UIImage imageNamed:@"banner1"],
                        [UIImage imageNamed:@"banner1"],
                        [UIImage imageNamed:@"banner1"],
                        [UIImage imageNamed:@"banner1"]
                        ];
    
    
    // 本地加载图片的轮播器
    _cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kWindowW, 200) imageNamesGroup:images];
    _cycleView.backgroundColor = [UIColor whiteColor];
    
    _cycleView.autoScrollTimeInterval = images.count;// 自定义轮播时间间隔
    
    _cycleView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    _cycleView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
    _cycleView.showPageControl = YES;
    _cycleView.pageControlDotSize = CGSizeMake(10, 30);
    _cycleView.pageDotColor = [UIColor grayColor];
    _cycleView.currentPageDotColor = [UIColor whiteColor];
    //NSArray*titles = @[@"山东华网智能科技股份有限公司",@"山东华网智能科技股份有限公司",@"山东华网智能科技股份有限公司"];
    //cycleScrollView.titlesGroup = titles;
    
    _cycleView.delegate = self;
    [self addSubview:_cycleView];
}

-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    NSLog(@"---点击了第%ld张图片", (long)index);
    
}

// kindBut
- (void)setUpKindButton{
    
    CGFloat butWidth = kWindowW/4.0;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        for (NSInteger i = 0; i < _kindNameArr.count; i++) {
            JProductTypeModel *model = _kindNameArr[i];
            
            CGRect rect = CGRectMake(butWidth * (i%4), (butWidth + 5) * (i/4) + 210, butWidth, butWidth);
            JButton *button = [JButton buttonWithType:UIButtonTypeCustom
                                                frame:rect
                                                title:model.Name
                                           titleColor:[UIColor blackColor]
                                            titleFont:16
                                        textAlignment:NSTextAlignmentCenter
                                                image:nil
                                 imageViewContentMode:UIViewContentModeScaleToFill
                                                  tag:i+10
                                             imageURL:model.ImgUrl
                                              handler:^(UIButton *sender) {
                                                  if (_btnBlock) {
                                                      _btnBlock(model);
                                                  }
                                              }];
            
            [self addSubview:button];
        }
        
        _minY = (butWidth + 5) * (_kindNameArr.count/4) + 210;
    });
    
    _scrBgView.frame = CGRectMake(0, _minY, kWindowW, miaoshaHeight);
//    if (_getViewHeight) {
//        _getViewHeight(_scrBgView.bottom);
//    }
}

- (void)setConfigBottomView:(NSArray *)killModelArr {
    
    NSMutableArray *imgURLArr = [NSMutableArray array];
    
    for (JKillProducModel *model in killModelArr) {
        [imgURLArr addObject:model.ImgUrl];
    }
    
    _isFirst = YES;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, _minY, kWindowW, miaoshaHeight)];
    view.backgroundColor = [UIColor whiteColor];
    _scrBgView = view;
    
    YYLabel *titleLabel = [YYLabel new];
    titleLabel.top = 0;
    titleLabel.left = 15;
    titleLabel.size = CGSizeMake(kWindowW - 30, 40);
    titleLabel.font = kFONT_TITLE(16);
    titleLabel.text = @"人气秒杀";
    
    
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, titleLabel.bottom, kWindowW, view.height - titleLabel.height)];
    scroll.backgroundColor = [UIColor whiteColor];
    scroll.showsHorizontalScrollIndicator = NO;
    CGFloat imgWidth = 90.0;
    
    [self addSubview:view];
    [view addSubview:titleLabel];
    [view addSubview:scroll];
    
    for (int i = 0; i < killModelArr.count; i++) {
        JKillProducModel *model = killModelArr[i];
        
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(15 + i * (imgWidth + 10) , 0, imgWidth, scroll.height)];
        UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURLArr[i]]]];
        imgView.image = img;
        imgView.userInteractionEnabled = YES;
        imgView.layer.cornerRadius = 5;
        imgView.layer.masksToBounds = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            if (_killBtnBlock) {
                _killBtnBlock(model);
            }
        }];
        [imgView addGestureRecognizer:tap];
        
        UILabel *label = [UILabel new];
        label.top = imgView.height - 20;
        label.left = 20;
        label.size = CGSizeMake(imgView.width, 20);
        label.text = model.PriceText;
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor redColor];
        [imgView addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.cornerRadius = 5;
        label.layer.masksToBounds = YES;
        label.font = kFONT_TITLE(14);
        CGSize size = [label sizeThatFits:CGSizeMake(imgView.width, 20)];
        label.frame = CGRectMake(imgView.width - size.width - 20, imgView.height - 20, size.width + 20, 20);
        
        [scroll addSubview:imgView];
    }
    
    scroll.contentSize = CGSizeMake(15 + killModelArr.count * (imgWidth + 10) , scroll.height);
    
    _lineView.left = 0;
    _lineView.top = _scrBgView.bottom;
    _lineView.size = CGSizeMake(kWindowW, 60);
    
    
}

- (void)loadAdviceArr
{
    NSString *url = @"Banner/GetShopBannerPictures";
    [JNetManager LoadAdviceImgUrl:url
                          success:^(id response) {
                              if (response) {
                                  NSMutableArray *imgArr = (NSMutableArray *)response;
                                  
                                  NSMutableArray *imgURLArr = [NSMutableArray array];
                                  
                                  
                                  dispatch_async(dispatch_get_global_queue(0, 0), ^{
                                      for (JAdviceModel *model in imgArr) {
                                          [imgURLArr addObject:model.ImgUrl];
                                      }
                                      _bannerArr = [NSArray arrayWithArray:imgURLArr];
                                      _cycleView.autoScrollTimeInterval = _bannerArr.count;
                                      
                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          _cycleView.imageURLStringsGroup = _bannerArr;
                                      });
                                      
                                  });
                    
                              }
                          } failure:^(NSString *err) {
                              [SVProgressHUD showErrorWithStatus:err];
                          }];
}

- (void)setKindNameArr:(NSArray *)kindNameArr
{
    _kindNameArr = kindNameArr;
    CGFloat butWidth = kWindowW/4.0;
    _minY = (butWidth + 5) * (_kindNameArr.count/4) + 210;
    // 种类按钮
    [self setUpKindButton];

}

- (void)setKillImgArr:(NSMutableArray *)killImgArr
{
    _killImgArr = killImgArr;
    [self setConfigBottomView:killImgArr];

}

- (void)loadKillView
{
    NSString *url = @"PopularKillApi/GetPopularKillProducts";
    [JNetManager LoadKillProductImgUrl:url
                          success:^(id response) {
                              if (response) {

                                  if (_isFirst) return;

                                  NSMutableArray *imgArr = (NSMutableArray *)response;

                                  NSMutableArray *imgURLArr = [NSMutableArray array];
                                  
                                  
                                  dispatch_async(dispatch_get_global_queue(0, 0), ^{
                                      for (JKillProducModel *model in imgArr) {
                                          [imgURLArr addObject:model.ImgUrl];
                                      }
                                      _killImgArr = [NSMutableArray arrayWithArray:imgURLArr];
                                      
                                      if (_killImgArr.count > 0) {
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              [self setConfigBottomView:imgArr];

                                          });
                                      }
                                      
                                      CGFloat butWidth = kWindowW/4.0;
                                      if (_getViewHeight) {
                                          
                                          CGFloat viewHeight = 210 + (butWidth + 5) * (_kindNameArr.count/4) + (imgArr.count > 0 ? 160 : 0) + 60;
                                          NSLog(@"viewHeight===%.2f",viewHeight);
                                          _getViewHeight(viewHeight);
                                      }
                                      
                                  });
                              }
                          } failure:^(NSString *err) {
                              [SVProgressHUD showErrorWithStatus:err];
                          }];
}

- (void)loadAdviceKindArr
{
    NSString *url = @"Categories/GetCategories";
    [JNetManager LoadKindAdviceImgUrl:url
                          success:^(id response) {
                              if (response) {
                                  NSMutableArray *imgArr = (NSMutableArray *)response;

                                  dispatch_async(dispatch_get_global_queue(0, 0), ^{

                                      _kindNameArr = [NSArray arrayWithArray:imgArr];
                                      CGFloat butWidth = kWindowW/4.0;
                                      _minY = (butWidth + 5) * (_kindNameArr.count/4) + 210;
                                      // 种类按钮
                                      [self setUpKindButton];
                                  });
                                  
                              }
                          } failure:^(NSString *err) {
                              [SVProgressHUD showErrorWithStatus:err];
                              
                          }];
}


@end
